<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/* 关于返回结果的json格式建议如下：
data:执行成功返回的数据
status:简单的表示业务执行的状态，如成功success，登录失败，更新失败等
code:业务执行状态的代码（可不设计）
message:表示详细信息
version:版本号 
*/


class MY_Controller extends CI_Controller
{

	var $_login_userid;

	function __construct()
	{
		parent::__construct();
		$this->_login_userid = $this->get_login_userid();
		$this->load->database();
		
	}

	//获得当前登录的用户id
	private function get_login_userid()
	{
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
		if ($this->tank_auth->is_logged_in()) {
			return $this->tank_auth->get_user_id();
		} else {
			return 0;
		}
	}

	//检测当前登录用户是否是某个团队的成员
	private function check_is_member($teamid)
	{
		$this->load->database();
		$this->db->where('tid', $teamid);
		$query = $this->db->get('team_user');
		if ($num_rows = $query->num_rows() > 0) {
			return true;
		} else {
			return false;
		}
	}
	//将数组转换成json，并输出
	protected function array_to_json($array)
	{
		$json_string = json_encode($array);
		echo $json_string;
	}

	//按给定的sql查询并输出json
	protected function query_to_json($sql)
	{
		if ($sql) {
		} else { //防止出现空查询条件
			$sql = "select 1 ";
		}
		$this->load->database();
		$query = $this->db->query($sql);
		if (($num_rows = $query->num_rows()) > 0) {
			$result_array = array(
				'data' => $query->result_array(),
				'status' => 'success',
				'message' => '查询到' . $num_rows . "条记录"
			);
		} else {
			$result_array = array(
				'status' => 'nofound',
				'message' => '消息' . $this->db->last_query()
			);
		}
		$this->array_to_json($result_array);
	}

	protected function htmlstr_loginbar(){
		$data=array();
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
		if ($this->tank_auth->is_logged_in()){
			$data['login_username']=$this->tank_auth->get_username();
		}else{
			$data['login_username']='';
		}
		$htmlstr=$this->load->view('htmlpage/loginbar',$data,true);
		return $htmlstr;
	}
	protected function get_share_pagedata(){
//		$pagedata['head']=$this->load->view('htmlpage/head','',true);
//		$pagedata['logobar']=$this->load->view('htmlpage/logobar','',true);
		$pagedata['loginbar']=$this->htmlstr_loginbar();
//		$pagedata['navbar']=$this->load->view('htmlpage/navbar','',true);
//		$pagedata['footbar']=$this->load->view('htmlpage/footbar','',true);
		return $pagedata;
	}

}